#include <QtGui>
#include <QX11Info>
#include <QtDebug>
#include <X11/Xlib.h>
#include <QApplication>
#include <QDesktopWidget>

int main(int argc, char *argv[])
{
    Window root_dummy, parent_dummy ; //useful when debugging
    Window* children ;
    uint numberOfChildren ;
    char *windowname ;
    char *windowmanname ;
    //XWindowAttributes attr;
    qDebug() << "Starting...";
    QApplication app(argc, argv);
    if( XQueryTree( QX11Info::display(), QX11Info::appRootWindow(), &root_dummy, &parent_dummy, &children, &numberOfChildren) == 0 )
    {
        ///@todo failed!
	qDebug() << "XQueryTree failed, expect whatever..." << endl ; 
    }

    qDebug() << "XQueryTree returned " << numberOfChildren << " top level windows, only those with names returned." ;

    for(unsigned int currentChildNumber=0; currentChildNumber < numberOfChildren; currentChildNumber++)
    {
	
        Window currentWindow = children[currentChildNumber] ;
        /*
            if( currentWindow == qapp::tb->winId() )
                continue;
            XGetWindowAttributes(QX11Info::display(), currentWindow, &attr);
        */
	
        if (0 != XFetchName( QX11Info::display(), currentWindow, &windowname) )
        {
            QString winName( windowname ) ;
            XGetWMName( QX11Info::display() , currentWindow, &windowmanname) ;
            QString anotherName( windowmanname ) ; 
            qDebug() << "Quering window #" << currentChildNumber << ":" << winName << " WM:" << anotherName;
            XFree( windowname ) ;
            XFree( windowmanname ) ;
        }
	else
	{
	  // qDebug() << "****XFetchName failed!" ;
	}
    }
    //XMapRaised (dpy, window);
    //XSync (dpy, False);
    //XSync(QX11Info::display(), FALSE);
    //XGetWMName();
    //XmbTextPropertyToTextList();

}
